package com.itheima.case2.web;

import com.itheima.case2.pojo.vo.Result;
import com.itheima.case2.pojo.po.Role;
import com.itheima.case2.service.RoleServiceImpl;
import com.itheima.case2.utils.BaseController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/role/*")
public class RoleServlet extends BaseServlet {

  /*  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求和响应编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //1.获取方法名
        String url = request.getRequestURI();
        int lastIndex = url.lastIndexOf('/');
        String method = url.substring(lastIndex + 1,url.length());

        if(method.equals("queryAll")) {
            //分页查询用户功能
            queryAll(request,response);
        }
    }*/

    protected void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            RoleServiceImpl service = new RoleServiceImpl();
            List<Role> list = service.findRoleList();

            BaseController.printResult(response,new Result(true,"查询角色成功",list));
        } catch (Exception e) {
            e.printStackTrace();
            BaseController.printResult(response, new Result(false, "查询角色失败!"));
        }
    }

}